package com.casework

import org.joda.time.LocalDate
import com.casework.auth.AuthUser
import grails.util.Environment


class SearchQuery {
    AuthUser user
    LocalDate dateOfSearch
    boolean searchCases = true
    String firms = ""
    Long ecNumber
    Integer fromDecisionYear
    Integer toDecisionYear
    List phase = []
    List relativeToSO = []
    List clusterSale = []
    List alternativeRemedy = []
    List purchaserCriteria = []
    String shortNace
    List competitionConcern = []
    String classification = ""
    String searchTerm = ""

    static constraints = {
        ecNumber nullable:true
        fromDecisionYear nullable:true
        toDecisionYear  nullable:true
        shortNace  nullable:true
        classification nullable:true
    }

    static hasMany = [relativeToSO:RelativeToSO,
            purchaserCriteria: PurchaserCriteria,
            competitionConcern: String,
            clusterSale: YesNoNA,
            alternativeRemedy: YesNoNA,
            phase: Integer]



    static void cloneThe(SearchCommand command, def user){
        if(Environment.current == Environment.TEST) return
        
        SearchQuery query = new SearchQuery()
        query.user = user
        query.searchTerm = command.textQuery
        query.dateOfSearch = new LocalDate()
        query.searchCases = command.cases
        query.firms = command.firmNames
        query.ecNumber = command.ecNumber
        query.shortNace = command.shortNace?.join(', ')
        query.fromDecisionYear = command.fromDecisionYear
        query.toDecisionYear = command.toDecisionYear
        query.phase = command.phase
        query.relativeToSO = command.relativeToSO
        query.clusterSale = command.clusterSale
        query.alternativeRemedy = command.alternativeRemedy
        query.purchaserCriteria = command.purchaserCriterias
        query.competitionConcern = command.competitionConcern
        query.classification = command.classification
        query.save(failOnError:true, flush:true)
    }
}
